package com.openwater.core;

import java.nio.FloatBuffer;

import android.opengl.GLES20;

import com.openwater.tools.BufferAllocator;
import com.openwater.tools.math.MatrixState;

/**
 * This model object file is export form 3d-max which is profession 3D-model building software.
 * It contain vertex coordinate,texture coordinate and normal vector.
 * @author fuxp
 * @date 2012-6-8
 * @version 1.0 Copyright Corporation 2013,fuxp, All Rights Reserved
 * @since
 */
public final class ModelObject {
    private int mProgram;
    private int muMVPMatrixHandle;
    private int muMMatrixHandle;
    private int maPositionHandle;
    private int maNormalHandle;
    private int maLightLocationHandle;
    private int maCameraHandle;
    private int maTexCoorHandle;

    private FloatBuffer mVertexBuffer;
    private FloatBuffer mNormalBuffer;
    private FloatBuffer mTexCoorBuffer;
    private int vCount = 0;

    public ModelObject(int programid, float[] vertices, float[] normals, float[] texCoors) {
        initVertexData(vertices, normals, texCoors);
        initShader(programid);
    }

    private void initVertexData(float[] vertices, float[] normals, float[] texCoors) {
        vCount = vertices.length / 3;
        mVertexBuffer = BufferAllocator.toFloatBuffer(vertices);
        mNormalBuffer = BufferAllocator.toFloatBuffer(normals);
        mTexCoorBuffer = BufferAllocator.toFloatBuffer(texCoors);
    }

    private void initShader(int programid) {
        mProgram = programid;
        maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
        maNormalHandle = GLES20.glGetAttribLocation(mProgram, "aNormal");
        muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
        muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
        maLightLocationHandle = GLES20.glGetUniformLocation(mProgram, "uLightLocation");
        maTexCoorHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoor");
        maCameraHandle = GLES20.glGetUniformLocation(mProgram, "uCamera");
    }

    public void drawSelf(final int texId) {
        GLES20.glUseProgram(mProgram);
        GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
        GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getViewProjMatrix(), 0);
        GLES20.glUniform3fv(maLightLocationHandle, 1, MatrixState.getPosLightBuf());
        GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.getCameraPosBuf());
        GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer);
        GLES20.glVertexAttribPointer(maNormalHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mNormalBuffer);
        GLES20.glVertexAttribPointer(maTexCoorHandle, 2, GLES20.GL_FLOAT, false, 2 * 4, mTexCoorBuffer);
        GLES20.glEnableVertexAttribArray(maPositionHandle);
        GLES20.glEnableVertexAttribArray(maNormalHandle);
        GLES20.glEnableVertexAttribArray(maTexCoorHandle);
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
    }
}
